﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.Composition;
using Core.Common;
using Core.Interfaces;
using Core.DataAccess;
using Core.BusinessLogic.Interfaces;
using SonCa.SocialNetwork.Web.Accounts.IPresenters;
using System.Web.UI.WebControls;
using SonCa.SocialNetwork.Web.Resources;

namespace SonCa.SocialNetwork.Web.Accounts.Presenters
{
    public class ProfileEditPresenter
    {
        private IProfileEdit _view;
        [Import]
        private IProfileEditService _profileEditService;
        [Import]
        private IUserSession _userSession;
        [Import]
        private IAccountService _accountService;
        [Import]
        private ISchoolService _schoolService;
        [Import]
        private IWorkPlaceService _workPlaceService;
        [Import]
        private ILocationService _locationService;

        public ProfileEditPresenter()
        {
            MEFManager.Compose(this);
        }

        public void Init(IProfileEdit View)
        {
            _view = View;                        
            _userSession.CurrentUser = _accountService.GetAccountByUsername(_userSession.UserName);
        }

        public List<string> GetCountryList()
        {
            return _profileEditService.GetCountryList();
        }

        public List<string> GetVietnameseCityList()
        {
            return _profileEditService.GetVietnameseCityList();
        }

        public List<string> GetJobFieldList()
        {
            return _profileEditService.GetJobFieldList();
        }

        public void LoadProfileInfo()
        {
            _view.LoadProfileInfo(_userSession.CurrentUser);
        }

        public void LoadEducation()
        {
            if (_userSession.CurrentUser != null)
            {
                List<AccountSchool> accSchoolList = _schoolService.GetAccountSchoolListByAccountID(_userSession.CurrentUser.AccountID);
                List<School> schoolList = _schoolService.GetSchoolsByAccountID(_userSession.CurrentUser.AccountID);
                _view.LoadEducation(accSchoolList,schoolList);
            }
        }

        public void LoadWorkPlace()
        {
            if (_userSession.CurrentUser != null)
            {
                List<AccountWorkPlace> accWorkplaceList = _workPlaceService.GetAccountWorkPlaceListByAccountID(_userSession.CurrentUser.AccountID);
                List<WorkPlace> workplaceList = _workPlaceService.GetWorkPlaceListByAccountID(_userSession.CurrentUser.AccountID);
                _view.LoadWorkPlace(accWorkplaceList, workplaceList);
            }
        }

        public void LoadLocation()
        {
            if (_userSession.CurrentUser != null)
            {
                List<AccountLocation> accLocationList = _locationService.GetAccountLocationByAccountID(_userSession.CurrentUser.AccountID);
                List<Location> locationList = _locationService.GetLocationListByAccountID(_userSession.CurrentUser.AccountID);
                _view.LoadLocation(accLocationList, locationList);
            }
        }

        public string UpdateProfileInfo(string fullName, string birthdate, string gender, string email, string about, string interests)
        {
            Account account = _userSession.CurrentUser;
            if (account != null)
            {
                string[] array = fullName.Split(new string[]{" "},StringSplitOptions.RemoveEmptyEntries);
                if (array.Length == 1)
                {
                    account.LastName = "";
                    account.MidName = "";
                    account.FirstName = array[0];
                }
                else if (array.Length == 2)
                {
                    account.LastName = array[0];
                    account.MidName = "";
                    account.FirstName = array[1];
                }
                else if (array.Length > 2)
                {
                    account.LastName = array[0];
                    account.MidName = fullName.Substring(fullName.IndexOf(' ') + 1,fullName.LastIndexOf(' ') - 1 - fullName.IndexOf(' '));
                    account.FirstName = fullName.Substring(fullName.LastIndexOf(' ') + 1);
                }
                account.BirthDate = Convert.ToDateTime(birthdate);
                account.Email = email;
                account.AboutMe = about;
                account.Interests = interests;
                account.LastUpdateDate = DateTime.Now;
                _accountService.SaveAccount(account);
                _userSession.CurrentUser = account;
                //_view.ChangeViewToggle(1);
                return "ok";
            }
            return "";
        }

        public string UpdateEducationInfo(List<School> schoolList, List<AccountSchool> accSchoolList)
        {
            Account account = _accountService.GetAccountByUsername(_userSession.UserName);
            if (account != null)
            {                                
                int update = _schoolService.UpdateSchoolInfo(_userSession.CurrentUser.AccountID, schoolList[0]);
                if (update != -1)
                {
                    update = _schoolService.UpdateAccountSchoolInfo(_userSession.CurrentUser.AccountID, accSchoolList[0],update);
                    if (update != -1)
                        return "ok";
                }
            }
            return "";
            //_view.ChangeViewToggle(2);
        }        

        public string UpdateWorkPlaceInfo(List<WorkPlace> workplaceList, List<AccountWorkPlace> awpList)
        {
            Account account = _accountService.GetAccountByUsername(_userSession.UserName);
            if (account != null)
            {
                int update = _workPlaceService.UpdateWorkPlaceInfo(_userSession.CurrentUser.AccountID, workplaceList[0]);
                if (update != -1)
                {
                    update = _workPlaceService.UpdateAccountWorkPlaceInfo(_userSession.CurrentUser.AccountID, awpList[0],update);
                    if (update != -1)
                        return "ok";
                }
            }
            return "";
            //_view.ChangeViewToggle(3);
        }

        public string UpdateLocationInfo(List<Location> locationList, List<AccountLocation> alList)
        {
            Account account = _accountService.GetAccountByUsername(_userSession.UserName);
            if(account != null)
            {
                int update = _locationService.UpdateLocationInfo(_userSession.CurrentUser.AccountID, locationList[0]);
                if (update != -1)
                {
                    update = _locationService.UpdateAccountLocationInfo(_userSession.CurrentUser.AccountID, alList[0], update);
                    if (update != -1)
                        return "ok";
                }
            }
            return "";
            //_view.ChangeViewToggle(0);
        }

        public ListItemCollection GetGradeList()
        {
            ListItemCollection result = new ListItemCollection();
            List<string> list = Enum.GetNames(typeof(GradeEnum)).ToList();
            foreach (string grade in list)
            {
                ListItem item = new ListItem(GradeResource.ResourceManager.GetString(grade), Convert.ToInt32(Enum.Parse(typeof(GradeEnum), grade, true)).ToString());
                result.Add(item);
            }
            return result;
        }
    }
}